from datetime import date, timedelta
from datetime import datetime
import random
import csv

def write_csv_cls(data):
    # 打开文件以写入模式
    with open('data/sale.csv', 'w', newline='') as file:
        writer = csv.writer(file)
        # 写入数据
        for row in data:
            writer.writerow(row)


def generate_dates(start_date_str, end_date_str, date_format='%Y-%m-%d'):
    # 将字符串转换为日期对象
    start_date = datetime.strptime(start_date_str, date_format)
    end_date = datetime.strptime(end_date_str, date_format)

    # 当前日期设置为开始日期
    current_date = start_date

    date_list = []
    # 当当前日期小于或等于结束日期时循环
    while current_date <= end_date:
        # 打印当前日期
        date_list.append(current_date.strftime(date_format))

        # 将当前日期增加一天
        current_date += timedelta(days=1)

    return date_list


def main():
    start_date = '2024-01-01'
    end_date = '2024-04-10'
    date_list = generate_dates(start_date, end_date)
    data_list = [
        ["Year", "Month", "Day", "Sales"]
    ]
    for dt in date_list:
        year = dt.split("-")[0]
        month = dt.split("-")[1]
        day = dt.split("-")[2]
        sale = random.randint(200,500)
        data_list.append([year, month, day, sale])
    write_csv_cls(data_list)

main()